<?php

class Home extends CI_Controller {

    public function __construct()

    {
        parent::__construct();
		$this->load->helper(array('form', 'url'));
		$this->load->library('session');	
		$this->load->database();
		$this->load->library('form_validation');
		$this->load->model('welcome_mdl');
		
    }

 

    /**

    * Load the main view with all the current model model's data.

    * @return void

    */

    public function index($param1 = '',$param2 = '')

    {
		
		
		if($param1 == 'add_new')
		{
			$data__header['page_name'] 		= 'add_new';
			$this->load->view('teacher/header',$data__header);	
        	$this->load->view('teacher/add_new');	
		}
		if($param1 == 'teacher')
		{
			$data__header['page_name'] 		= 'teacher';
			$this->load->view('teacher/header',$data__header);	
        	$this->load->view('teacher/teacher');	
		}
		if($param1 == 'student')
		{
			$data__header['page_name'] = 'student';
			$this->load->view('teacher/header',$data__header);	
        	$this->load->view('teacher/student');	
		}
		if($param1 == 'hierarchy')
		{
			$data__header['page_name'] = 'hierarchy';
			$this->load->view('teacher/header',$data__header);	
        	$this->load->view('teacher/hierarchy');	
		}
		if($param1 == 'hierarchy_show')
		{
			$data__header['page_name'] = 'hierarchy_show';
			$this->load->view('teacher/header',$data__header);	
        	$this->load->view('teacher/hierarchy_show');	
		}
		if($param1 == 'library_material')
		{
			$data__header['page_name'] = 'library_material';
			$this->load->view('teacher/header',$data__header);	
        	$this->load->view('teacher/library_material');	
		}
		if($param1 == 'need_to')
		{
			$data__header['page_name'] = 'need_to';
			$this->load->view('teacher/header',$data__header);	
        	$this->load->view('teacher/did_not');	
		}
		if($param1 == 'submitted_exams')
		{
			$data__header['page_name'] = 'exam_submitted';
			$this->load->view('teacher/header',$data__header);	
        	$this->load->view('teacher/submitted_exams');	
		}
		
    }//index
	public function need_to()
	{
			$data__header['page_name'] = 'need_to';
			$this->load->view('teacher/header',$data__header);	
        	$this->load->view('teacher/did_not');		
	}
	public function check_exam( $exam_id = null , $student_id = null)
	{
			$data["exam_id"] 		=  $exam_id;
			$data["student_id"] 	=  $student_id;
			$data__header['page_name'] = 'exam_check';
			$this->load->view('teacher/header',$data__header);	
        	$this->load->view('teacher/exam_check',$data);
	}
	public function add_new_form()
	{
		$add_board_data = $_POST['add_board'];
		$data_submitted = 0;
		foreach($add_board_data as $key => $board)
		{
			if( trim($board) != '') {
			$this->db->insert('tbl_courses',array('name' => $board , 'type' => 'board'));
			$data_submitted++;
			}
		}
		$add_class_data = $_POST['add_class'];
		foreach($add_class_data as $key => $class)
		{
			if( trim($class) != '') {
			$this->db->insert('tbl_courses',array('name' => $class , 'type' => 'class'));
			$data_submitted++;
			}
		}
		$add_subject_data = $_POST['add_subject'];
		foreach($add_subject_data as $key => $subject)
		{
			if( trim($subject) != '') {
			$this->db->insert('tbl_courses',array('name' => $subject , 'type' => 'subject'));
			$data_submitted++;
			}
		}
		if($data_submitted >0) {
			$this->session->set_flashdata('message_board_added', 1);
		}
		else
		{
			$this->session->set_flashdata('message_board_added', 0);
		}
		redirect('teacher/home/add_new');
		//tbl_courses
	}

 public function hieraechy_form()
 {
	 $data = array();
	 
	 	 $class_rooms 	= $_POST['course_class'];
		 $board 		= $_POST['course_board'];
		 $subject 		= $_POST['course_subject'];
	  foreach($class_rooms as $key => $class_room)
		{
			if($this->db->get_where("tbl_class",array("class_id"=>$class_room,"board_id"=>$board[$key]))->num_rows() !== 1 )
			{				
				$data[$key]['class_id'] = $class_room;
				$data[$key]['board_id'] = $board[$key];
				$data[$key]['subject_id'] = implode(",",$subject[$key]);
				$this->db->insert('tbl_class',$data[$key]);
			}
		}

	$this->session->set_flashdata('message_board_added', 'Added.');
	redirect('teacher/home/hierarchy');
 }
 public function add_exam()
 {
			$data__header['page_name'] = 'exams_add';
			$this->load->view('teacher/header',$data__header);	
        	$this->load->view('teacher/exam_add_new');	
 }

 public function submit_exam()
 {
		$exam_id		=  $_POST["exam_id"];
		$student_id		=  $_POST["student_id"];
		$answers		=  $_POST["answer"];
		$question_types	=  $_POST["question_type"];
		$questions		=  $_POST["question"];
		$comments		=  $_POST["answer_comment"];
		foreach($answers as $key => $answer)
		{
			$data_sub 						= 	array();
			$data_sub["exam_id"] 			=	$exam_id ;			
			$data_sub["question"] 			=	$questions[$key] ;
			$data_sub["answer"] 			=	$answers[$key] ;
			$data_sub["comments"] 			=	$comments[$key] ;
			if($question_types[$key] == "multiple")
			{
				$ker = "";
				foreach($data_sub["answer"] as $ans)
				{
					$ker.= $ans.",";
				}
				$data_sub["answer"] = $ker;
				$data_sub["status"] 			=	1 ;
				$this->db->where(array("exam_id"=>$exam_id,"student_id" => $student_id,"question"=>$questions[$key]));
				$this->db->update("submitted_by_students",$data_sub);
			}
			else
			{
				$data_sub["status"] 			=	1 ;
				$this->db->where(array("exam_id"=>$exam_id,"student_id" => $student_id,"question"=>$questions[$key]));
				$this->db->update("submitted_by_students",$data_sub);
			}		
		}
		$data_sub 						= array();
		$data_sub["exam_id"] 			=	$exam_id ;
		$data_sub["submitted_by_user"] 	=	$student_id;
		$data_sub["status"] 			=	1;
		$this->db->update("exam_submits",$data_sub);
		$this->session->set_flashdata("good_company",1);
		
		redirect("teacher/home/need_to");
 }
 public function publish($id = null)
 {
	 if( $id != null )
	 {
		$data["published"] = 1;
		$this->db->where("id",$id);
		$this->db->update("tbl_exams",$data);
		redirect('teacher/home/all_exam');
	 }	
 }
 public function add_question($id = null)
 {
	 if($id == null)
	 {
		 redirect('teacher/home/all_exam');
	 }
			$data__header["page_name"] = "add_question";
			$data["param2"] = $id;
			$this->load->view('teacher/header',$data__header);	
        	$this->load->view('teacher/add_question',$data);	
 }
 public function add_question_form()
 {
	 $qtype 							= 	$_POST["qtype"];
	 $question_type 					= 	'';
	 $as_answer 						= 	array();
	 $as_correct						=	array();
	 if($qtype == "1")
	 {
		 $question_type 				= 	"true_false";
		 $answer 						= 	$_POST["answer1"];
		 if($answer == "0")
		 {
			 $answer 					= 	"false";
			 $as_answer[] 				= 	$answer;
		 }
		 if($answer == "1")
		 {
			 $answer 					= 	"true";
			 $as_answer[] 				= 	$answer;
		 }
		  $as_correct[] 				= 	1;
	 }
	 elseif($qtype == "2")
	 {
		 $question_type 				= 	"single";
		 $answers 						= 	$_POST["answer2"];
		 $corrects 						= 	$_POST["correct2"];
		 foreach($answers as $key => $answer)
		 {
			 $as_answer[$key] 			= $answer;
			 $as_correct[$key] 			= (int)$corrects[$key];
		 }
	 }
	 elseif($qtype == "3")
	 {
		 $question_type 				= 	"multiple";
		 $answers 						= 	$_POST["answer3"];
		 $corrects						= 	$_POST["correct3"];
		  foreach($answers as $key => $answer)
		 {
			 $as_answer[$key] 			= $answer;
			 $as_correct[$key] 			= (int)$corrects[$key];
		 }
	 }
	 elseif($qtype == "4")
	 {
		 $question_type 				= 	"subjective";
		 $answer  						= 	'';
		 $correct  						= 	'';
		 $as_answer[] 					= $answer;
		 $as_correct[] 					= 0;
	 }
	 
	 $question 							= 	$_POST["question"];
	 $exam_id 							= 	$_POST["exam_no"];
	 $marks 							= 	$_POST["question_marks"];
	 
	 $data_question 					= 	array();
	 $data_question["exam_id"]  		=   (int) $exam_id;
	 $data_question["question_text"]  	=   $question;
	 $data_question["question_type"]  	=   $question_type;
	 $this->db->insert("tbl_exam_questions",$data_question);
	 $question_id 						=   $this->db->insert_id();
	 
	 $ans_ids 							= 	array();
	 $data_answer 						= 	array();
	 $data_answer["exam_id"]  			=   (int)$exam_id;
	 $data_answer["question_id"]  		=   (int)$question_id;
	 
	 foreach( $as_answer as $key2 => $ans)
	 {
		 $data_answer["answer_text"]	=	$ans;	
		 $data_answer["correct"]		=	$as_correct[$key2];
			if($data_answer["answer_text"] != "")
			{
			 $this->db->insert("tbl_exam_answers",$data_answer);
			 $ans_ids[] 					=  $this->db->insert_id();
			}
	 }
	 $this->session->set_flashdata("added_question",1);
	 redirect("teacher/home/add_question/".(int)$exam_id);
 }
 public function add_new_exam()
 {	 
	 if($_POST)
	 {
		 $data = array();
		 $data["exam_title"] 		= $_POST["exam_title"];
		 $data["class_id"] 	 		= $_POST["course_class"];
		 $data["subject_id"] 		= $_POST["course_subject"];
		 $data["board_id"] 			= $_POST["course_board"];
		 $data["description"] 		= $_POST["description"];
		 $data["published"] 		= 0;
		 $data["no_of_questios"] 	= 0;
		 $data["total_marks"] 		= 0;
		 $this->db->insert("tbl_exams",$data);
		 $this->session->set_flashdata('message_board_added', 1);
		 redirect('teacher/home/add_exam');	
	 }
	 else
	 {
		  $this->session->set_flashdata('message_board_added', 0);
		  redirect('teacher/home/add_exam');
	 }	
 }
 public function all_exam()
 {
			$data__header['page_name'] = 'exams_all';
			$this->load->view('teacher/header',$data__header);	
        	$this->load->view('teacher/exam_all');	
 }
 public function library_material()
 {
	 if(isset($_POST['submitted']))
	 {
		$data["board"] = $_POST["board"];
		$data["class"] = $_POST["class"];
		$data["subject"] = $_POST["subject"];
		$data["all_noti"] = $_POST["all"];
		$data['title'] = $_POST['title'];
		$this->db->insert('tbl_library',$data);
		$this->session->set_flashdata('message_board_added', 'Added.');
		redirect('teacher/home/library_material');
	 }
	 else
	 {
		 $data__header['page_name'] = 'library_material';
			$this->load->view('teacher/header',$data__header);	
        	$this->load->view('teacher/library_material');	
	 }
 }
 public function schedule()
 {
	 if(isset($_POST['submitted']))
	 {
		$data["date"] 			= $_POST["date"];
		$data["schedule_title"] = $_POST["schedule_title"];
		$data["content"] 		= $_POST["content"];
		$this->db->insert('tbl_schedule',$data);
		$this->session->set_flashdata('message_board_added', 'Added.');
		redirect('teacher/home/schedule');
	 }
	 else
	 {
		 $data__header['page_name'] = 'schedule';
			$this->load->view('teacher/header',$data__header);	
        	$this->load->view('teacher/schedule');	
	 }
 }
 public function notification()
 {
	 if(isset($_POST['submitted']))
	 {
		$data["title"] 			= $_POST["title"];
		$data["details"] 		= $_POST["details"];
		$this->db->insert('tbl_notification',$data);
		$this->session->set_flashdata('message_board_added', 'Added.');
		redirect('teacher/home/notification');
	 }
	 else
	 {
			$data__header['page_name'] = 'notification';
			$this->load->view('teacher/header',$data__header);	
        	$this->load->view('teacher/notification');	
	 }
 }



}